package battleship.cxbr;

import java.util.ArrayList;

import battleship.emss.*;

public class ProtectContext extends Context
{
	protected int x;
	protected int y;
	protected double st=-Integer.MAX_VALUE;
	
	public ProtectContext(String n,CxBRAgent a)
	{
		super(n,a);
	}
	
	//How to get here from above
	public double ParentRule(double Time)
	{
		
		return 0.0;
	}
	//How to get here from side
	public double SiblingRule(double Time)
	{
		//Receives communications communications
		//might want to transition contexts
		if(Agent.incomingMessages.size()>0)
		{
			for(int i=0;i<Agent.incomingMessages.size();i++)
			{
				//System.out.println("Messages: "+Agent.incomingMessages.get(i).message);
				if(Agent.incomingMessages.get(i).message=="Active Engagement")
				{
					st=Time;
					return 0.7;
				}
			}
		}
		//hour of non detection
		if(Time-st<3600)
		{
			return 0.7;
		}
		else
		{
			return 0.0;
		}
	}
	
	//Things to do when just entering
	public void Initialize(double Time)
	{

	}

	
	//What do actually do
	public void Action(double Time)
	{
		Agent.status.status=Name;
		Agent.actions.clear();
		double distance=0;
		
		//detected others
		for(int i=0;i<Agent.detectedStatus.size();i++)
		{
			Status tmpStatus=Agent.detectedStatus.get(i);
			//not on my team, and a carrier
			
			if(tmpStatus.team==Agent.status.team && tmpStatus.type==Agent.CARRIER)
			{
				Agent.status.status+=" Protecting";
				x=tmpStatus.x;
				y=tmpStatus.y;
				distance=AgentUtility.getDistance(Agent.status, tmpStatus);
			}
		}
		//steaming forward,
		if(distance>10*1600)
		{
			Agent.actions.add(new Move(Agent.status.x,Agent.status.y,x,y,12));
			st=Time;
		}
		
		Agent.incomingMessages.clear();
		
				
		return;
	}
	
}

